package com.laolang.shop.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laolang.shop.common.core.util.CommonUtil;
import com.laolang.shop.modules.sys.entity.SysMenu;
import com.laolang.shop.modules.sys.entity.SysRoleMenu;
import com.laolang.shop.modules.sys.mapper.SysMenuMapper;
import com.laolang.shop.modules.sys.mapper.SysRoleMenuMapper;
import com.laolang.shop.modules.sys.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author khlbat
 * @version 1.0
 * @date 2020/11/1 20:46
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public boolean save(SysRoleMenu entity) {
        entity.setCreateTime(LocalDateTime.now());
        entity.setVersion(1);
        return super.save(entity);
    }

    @Override
    public List<SysMenu> listByUserId(Long userId) {
        if (CommonUtil.isAdmin(userId)) {
            return sysMenuMapper.selectList(Wrappers.lambdaQuery());
        }
        return sysRoleMenuMapper.listByUserId(userId);
    }
}
